<template>
  <zhlDialog
    ref="refcustomfromdialog"
    class="my-dialog"
    :width="width"
    :title="dialogtitle"
  >
    <div id="previewMain">
      <previewpage
        ref="previewPagemain"
        :postData="postData"
        :propsData="propsData"
        @initFrom="initFrom"
      ></previewpage>
      <div>
        <el-form  :model="auditform" label-width="120px">
            <el-row>
              <el-form-item label="审核状态" prop="auditflag">
                  <el-input
                    v-model="auditform.auditflag"
                    placeholder=""
                    :readonly="true"
                    size="small"
                  />
              </el-form-item>
            </el-row>
          <el-row>
            <el-form-item label="辅助信息" prop="auditauxiliary" >
              <el-input
                v-model="this.auditauxiliary"
                placeholder=""
                :readonly="true"
                size="small"
                class="elred"
              />
            </el-form-item>
          </el-row>
            <el-row>
              <el-form-item label="不通过原因" prop="rejectReason">
<!--                  <el-select v-model="auditform.rejectReason" placeholder="请选择不通过原因" clearable size="medium" @change="rejectReasonSelectChanged">-->
<!--                    <el-option-->
<!--                      v-for="dict in reasonOptions"-->
<!--                      :key="dict.dictValue"-->
<!--                      :label="dict.dictLabel"-->
<!--                      :value="dict.dictValue"-->
<!--                    />-->
<!--                  </el-select>-->
                <el-checkbox-group v-model="auditform.reasonSelect" size="mini"  @change="rejectReasonSelectChanged">
                  <el-checkbox-button v-for="dict in reasonOptions" :label="dict.auditReason" :key="dict.auditReason">{{dict.auditReason}}</el-checkbox-button>
                </el-checkbox-group>
              </el-form-item>
            </el-row>
            <el-row>
              <el-form-item label="审核备注" prop="nickName">
                <el-input v-model="auditform.auditremark"   type="textarea" :autosize="{ minRows: 4, maxRows: 4 }" placeholder="审核备注" />
              </el-form-item>
            </el-row>

          </el-form>
          <div slot="footer" class="dialog-footer">
          <el-button :disabled="autditDisabled" type="primary" @click="submitAuditSuccess">审核通过</el-button>
          <el-button :disabled="autditDisabled" type="primary" @click="submitAuditFalier">审核不通过</el-button>
          <el-button @click="clasedialog">关 闭</el-button>
          </div>
      </div>
      <div>
        <el-table :data="auditprocessList">
          <el-table-column label="审核人" align="center" prop="auditbyname" />
          <el-table-column label="审核状态" align="center" prop="auditflag" />
          <el-table-column label="审核备注" align="center" prop="auditremark"/>
          <el-table-column label="审核时间" align="center" prop="createdate" />
        </el-table>
      </div>
    </div>


  </zhlDialog>

</template>
<script>
import previewpage from '@/views/index.vue'
import { getFromInfo, saveConfigQueryJson,importTemplate,auditOperator,getAuditprocessByFid } from '@/api/tappfrom.js'
import { apiComm } from '@/api/index.js'
import { listAuditresonAll} from "@/api/grant/auditreson";
import { listAuditauxiliaryAll} from "@/api/grant/auditauxiliary";

export default {
  props: ['dialogclose'],
  name: 'auditfromdialog',
  data() {
    return {
      auditauxiliary:'',  //审核辅助信息
      auditprocessList:[],
      reaudit:false,
      fromid: '',
      businessid: '',
      nextflag:false,
      js: '',
      css: '',
      html: '',
      propsData: {},
      reasonOptions:[],
      from: undefined,
      autditDisabled:false,
      formData:{},
      visible: false,
      width: '70%',
      dialogtitle: '审核信息',
      postData: {
        type: 'refreshFrame',
        data: {
          generateConf: {
            fileName: '测试',
            type: 'file'
          },
          html: this.html,
          js: '{}',
          css: this.css,
          scripts: [],
          links: []
        }
      },
      auditform:{
        title: '信息审核',
        auditremark: '',
        $tableNewId: '',
        formid: this.fromid,
        rejectReason:'',
        questionName: '',
        auditflag: '',
        reasonSelect:[]
      },

    }
  },
  components: { previewpage },
  methods: {
    rejectReasonSelectChanged()
    {
      this.auditform.auditremark = this.auditform.reasonSelect.toLocaleString().replaceAll(",","，")
    },
    submitAuditSuccess(){
      this.auditform.auditflag ="审核通过";
      auditOperator(this.auditform).then(res =>{
        if(res.code==0)
        {
          if(this.reaudit)
          {
              this.clasedialog();
          }else
          {
            this.$message.success("审核操作成功，提取下一条");
            this.nextflag = true;
            this.opendialog(this.fromid, res.data,this.auditform.questionName)
          }
        }else if(res.code==-2)
        {
          this.auditform.auditflag ="审核中";
          this.$message.success("审核操作失败，错误信息："+res.msg);
        }else{
          this.$message.success("审核操作成功，提取记录失败，失败原因："+res.msg);
        }
      })
    },
    submitAuditFalier(){
      if(this.auditform.auditremark===''){
        this.$message.error('审核不通过时，审核备注不能为空')
        return;
      }
      this.auditform.auditflag ="审核失败";
        auditOperator(this.auditform).then(res =>{
          if(res.code==0)
          {
            if(this.reaudit)
            {
                this.clasedialog();
            }else
            {
              this.$message.success("审核操作成功，提取下一条");
              this.nextflag = true;
              this.opendialog(this.fromid, res.data,this.auditform.questionName)
            }
          }else if(res.code==-2)
          {
            this.auditform.auditflag ="审核中";
            this.$message.success("审核操作失败，错误信息："+res.msg);
          }else{
            this.$message.success("审核操作成功，提取记录失败，失败原因："+res.msg);
          }
      })
    },
    liulanfrom() {
      this.formData = {}
      this.autditDisabled = false

      if (this.fromid === undefined || this.fromid === null) {
        this.$message.error('表单ID找不到！')
        return false
      }
      // this.getDicts("audit_type_"+this.fromid).then(response => {
      //   this.reasonOptions = response.data;
      // });
      let resonQuery = {};
      resonQuery['tablename'] = this.auditform.questionName;
      listAuditresonAll(resonQuery).then(response => {
        this.reasonOptions = response.data;
      });

      this.propsData.businessid = this.businessid
      apiComm('tappFrom.getFromContent', { fid: this.fromid }).then(res => {
        if (res.code === 0) {
          if (res.data.length === 0) {
            this.$message.error('找不到表单信息，可能表单已删除！')
            return
          }
          this.dialogtitle = res.data[0].fromname
          this.width = res.data[0].dialogwidth
          const fromcontent = JSON.parse(res.data[0].fromcontent)
          this.js = fromcontent.js
          this.css =
            fromcontent.css !== false
              ? fromcontent.css + fromcontent.systemcss
              : fromcontent.systemcss
          this.propsData.IsPower = '1'
          this.html = fromcontent.html
          this.postData.data.css = this.css
          this.postData.data.html = this.html
          this.postData.data.js = this.js.replace('export default', '')
          this.$refs.refcustomfromdialog.visible = true
          if(this.nextflag)
          {
            this.$nextTick(() => {
              this.$refs.previewPagemain.previewinit(this.postData)
            })
          }
        } else {
          this.$message.error(res.msg)
        }
      })
      if(!this.nextflag){
        window.hideButton = function()
        {

            var button = $("#previewMain").find("button")

            if(button.length==0)
            {
              setTimeout("hideButton()", 100 );
            }
            else{
              $($($(button[0]).parent()).parent()).hide();
            }

        }
        hideButton()
      }
    },
    initFrom(from) {
      this.from = from
      const fromdata = from.data()
      fromdata.deviceMode = 'dialog'
      fromdata.dialogclose = this.dialogclose
      this.formData = fromdata;
      function data() {
        return fromdata
      }
      this.from.data = data

    },
    opendialog(fromid, businessid,questionName,reaudit) {
      if (fromid !== undefined && fromid !== '') {
        this.fromid = fromid
      }
      if (businessid !== undefined) {
        this.businessid = businessid
      }
      this.auditform.formid = this.fromid
      this.auditform.questionName = questionName
      this.auditform.$tableNewId = this.businessid
      this.liulanfrom();
      this.loadAuditprocessData();
      if(reaudit==null||reaudit==undefined)
      {
        this.reaudit = false;
      }else{
        this.reaudit = reaudit;
      }
    },
    clasedialog() {
      this.$refs.refcustomfromdialog.visible = false
    },
    loadAuditprocessData(){
      getAuditprocessByFid(this.auditform).then(res =>{
        this.auditprocessList = res.data;
      })
    }
  },
  created() {

  },
  watch: {
    businessid(val) {
      this.propsData.businessid = val
    },
    'formData.formData.auditflag'(newVal, oldVal) {
       if((newVal=='')||(newVal==null))
       {
         return;
       }
       if(this.formData.formData!=null)
       {
          this.auditform.auditremark = this.formData.formData.auditremark
         let resonQuery={}
         let zjhmcolumn = localStorage.getItem("zjhmcolumn");
         console.log(this.formData.formData);
         resonQuery['zjhm']=this.formData.formData[zjhmcolumn];
         resonQuery['tablename'] = this.auditform.questionName;
         listAuditauxiliaryAll(resonQuery).then(response => {
           if(response.data.length==0) {
             this.auditauxiliary = ''
           }else {
             this.auditauxiliary = response.data[0].auditAuxiliaryInfo;
           }
         });
       }
       this.auditform.auditflag = newVal
       hideButton()
      //如果可以重复审核，只有未审核情况下不允许审核
      if(newVal=='审核中')
      {
        this.autditDisabled = false
      }else
      {
        this.autditDisabled = true
      }

    },
  }
}
</script>
<style scoped>
#previewMain {
  padding: 10px;
}
.my-dialog >>>.ant-modal-body{
  display: flex;
}
.elred>>>input{
  color:red;
}
</style>
